package com.toman.widget;

import android.content.Context;
import android.view.LayoutInflater;

import com.toman.Util.ToastUtil;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhy on 16/4/9.
 */
public abstract class CommonAdapterNew<T> extends MultiItemTypeAdapter<T> {
    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected List<T> tDatas = new ArrayList<>();
    protected List<T> nDatas = new ArrayList<>();
    protected LayoutInflater mInflater;

    public CommonAdapterNew(final Context context, final int layoutId, List<T> datas) {
        super(context, datas);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;

        addItemViewDelegate(new ItemViewDelegate<T>() {
            @Override
            public int getItemViewLayoutId() {
                return layoutId;
            }

            @Override
            public boolean isForViewType(T item, int position) {
                return true;
            }

            @Override
            public void convert(ViewHolder holder, T t, int position) {
                CommonAdapterNew.this.convert(holder, t, position);
            }
        });
    }

    public void notifyDataSetChanged(int num) {
        nDatas.clear();
        tDatas = getDatas();
        if (num > getItemCount()) {
            num = getItemCount();
        }

        for (int i = 0; i < num; i++) {
            nDatas.add(getDatas().get(i));
        }
        mDatas = nDatas;
        notifyDataSetChanged();


    }

    public void notifyBack() {
        mDatas = tDatas;
        notifyDataSetChanged();


    }


    protected abstract void convert(ViewHolder holder, T t, int position);


}
